package VerticalPrototypes;

import java.sql.*;

/**
 * @author Tyler
 */

//Class for delete an item to an existing user account's inventory.
public class DeleteItemFromInventory {
    
    //Instance of this class being used.
    private static DeleteItemFromInventory instance = null;
    
    public DeleteItemFromInventory()
    {
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;

        //Hardcoded database location.
        String url = "snyamathi.dyndns.org:37965";
        String user = "root";
        String password = "baconatmidnight";
        
        //Hardcoded prototype user_id and item_id.
        int user_id = 1;
        int item_id = 1;

        try {
            //Connect to the databse.
            con = DriverManager.getConnection(url, user, password);
            st = con.createStatement();

            //Delete passed item_id from user_id's inventory.
            //Check to see if it exists in the user's inventory.
            if(rs == (st.executeQuery("SELECT * FROM userinventory WHERE userID = " + user_id)))
            {
				//Tyler's manual test: DELETE FROM userinventory WHERE itemID = 1 AND userID = 1;
                rs = st.executeQuery("DELETE FROM userinventory WHERE itemID = " + item_id + "AND userID = " + user_id);
            }
            if (rs.next()) {
                System.out.println(rs.getString(1));
            }

        } catch (SQLException ex) {
                //Something happens.
        } finally {
            //Close connections safely - May not need to do this? Overkill?
            try {
                if (rs != null) {
                    rs.close();
                }
                if (st != null) {
                    st.close();
                }
                if (con != null) {
                    con.close();
                }

            } catch (SQLException ex) {
                    //Something happens.
            }
        }
    }
    
    //Execute the deletion of item to inventory from user side.
    public static DeleteItemFromInventory user_DeleteItemFromInventory()
    {
        if(instance == null)
            instance = new DeleteItemFromInventory();
        return instance;
    }  
}